package com.ricky.base.bean;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ricky.admin.util.LoginUserUtils;
import com.ricky.base.tool.target.comment.IgnoreField;
import com.ricky.base.tool.target.comment.OComment;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Date;

/**
 * @Author Ricky
 * @Date 2024/6/20 14:08
 * @Description: 公共实体类
 */
@MappedSuperclass
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
//    @Column(name = "id", unique = true, nullable = false, columnDefinition = "char(char32) COMMENT '主键id'", insertable = true, updatable = true, length = 32)
    @Column(name = "id", unique = true, nullable = false, columnDefinition = "varchar(32)", length = 32)
    @IgnoreField
    public String id;
    //    @CreatedDate
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
//    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @Column(name = "create_time",columnDefinition = "bigint COMMENT '创建时间'")
    @OComment("创建时间")
    public Long createTime = System.currentTimeMillis();
    @OComment("修改时间")
    public Long updateTime;
    @OComment("创建人id")
    @Column(columnDefinition = "varchar(32)")
    public String createUserId;
    @OComment("创建人名称")
    @Column(columnDefinition = "varchar(32)")
    public String createUserName;
    @OComment("修改人id")
    @Column(columnDefinition = "varchar(32)")
    public String updateUserId;
    @OComment("修改人名称")
    @Column(columnDefinition = "varchar(32)")
    public String updateUserName;


    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return this.createTime != null ? new Date(this.createTime) : null;
    }

    public void setCreateTime(Object createTime) {
        try {
            this.createTime = ObjectUtil.isNotEmpty(createTime) ? Long.parseLong(createTime.toString()) : System.currentTimeMillis();
        } catch (Exception e) {
        }
    }
//    public void setCreateTime(Object createTime) {
//    }

    public Long getLongCreateTime() {
        return this.createTime;
    }

    public Long getLongUpdateTime() {
        return this.updateTime;
    }

    public Date getUpdateTime() {
        return this.updateTime != null ? new Date(this.updateTime) : null;
    }

    public void setUpdateTime(Object updateTime) {
        this.updateTime = System.currentTimeMillis();
    }

    public String getCreateUserId() {
        return createUserId;
    }

    public void setCreateUserId(String createUserId) {
        try {
            this.createUserId = createUserId != null ? createUserId : LoginUserUtils.getUserId();
        } catch (Exception e) {
        }
    }

    public String getCreateUserName() {
        return createUserName;
    }

    public void setCreateUserName(String createUserName) {
        try {
            this.createUserName = createUserName != null ? createUserName : LoginUserUtils.getUserName();
        } catch (Exception e) {
        }
    }

    public String getUpdateUserId() {
        return updateUserId;
    }

    public void setUpdateUserId(String updateUserId) {
        try {
            this.updateUserId = LoginUserUtils.getUserId();
        } catch (Exception e) {
        }
    }

    public String getUpdateUserName() {
        return updateUserName;
    }

    public void setUpdateUserName(String updateUserName) {
        try {
            this.updateUserName = LoginUserUtils.getUserName();
        } catch (Exception e) {
        }
    }
}

